﻿using Microsoft.EntityFrameworkCore;
using SJ.Platform.Common;
using SJ.Platform.EFCore.Model;
using SJ.Platform.EFCore.Model.FlowManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SJ.Platform.EFCore.DBOperation.DBOperation.MySQL.Flow
{
    public class Flow_MainHandle
    {
        private readonly SJDBContext _context;

        public Flow_MainHandle(SJDBContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> CreateAsync(FlowMain entity)
        {
            try
            {
                _context.FlowMains.Add(entity);
                await _context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_MainHandle=>Create", ex);
                return false;
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(FlowMain entity)
        {
            try
            {
                _context.Entry(entity).State = EntityState.Modified;
                await _context.SaveChangesAsync();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_MainHandle=>Update", ex);
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(FlowMain entity)
        {
            try
            {
                _context.FlowMains.Remove(entity);
                return _context.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_MainHandle=>Delete", ex);
                return false;
            }
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteByIdAsync(string id)
        {
            try
            {
                var entity = _context.FlowMains.Find(id);
                if (entity != null)
                {
                    _context.FlowMains.Remove(entity);
                    await _context.SaveChangesAsync();
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_MainHandle=>DeleteById", ex);
            }
            return false;
        }


        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<FlowMain> GetEntityByIdAsync(string id)
        {
            try
            {
                return await _context.FlowMains.FindAsync(id);
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_MainHandle=>GetEntityById", ex);
            }
            return null;
        }


        /// <summary>
        /// 分页获取所有流程
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public async Task<(List<FlowMain> flows, int count)> GetAllAsync(int pageNumber, int pageSize, Expression<Func<FlowMain, bool>> condition = null)
        {
            try
            {
                var query = _context.FlowMains.AsQueryable();

                // 如果提供了查询条件，则应用它
                if (condition != null)
                {
                    query = query.Where(condition);
                }

                //总条数
                int count = await query.CountAsync();

                // 应用分页
                var skip = (pageNumber - 1) * pageSize;
                query = query.OrderByDescending(order => order.AddTime).Skip(skip).Take(pageSize);//倒序排列

                // 执行查询并返回结果
                var flows = await query.ToListAsync();
                //List<FlowMainExtend> flows = new List<FlowMainExtend>();
                //if (flowList != null && flowList.Count > 0)
                //{
                //    flows = flowList.MapToList<FlowMain, FlowMainExtend>();
                //}
                return (flows, count);
            }
            catch (Exception ex)
            {
                LogManager.WriteError("Flow_MainHandle=>GetAllAsync  page", ex);
            }
            return (null, 0);
        }
    }
}
